﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using System.Data.Linq;
using System.Linq.Expressions;
using System.Diagnostics;
using System.Data.Linq.Mapping;
using System.Collections;
using Entity;
using System.Reflection;
using System.Configuration;
namespace Data.Car_Rental
{
    /// <summary>
    /// 某个项目的数据库操作基类
    /// </summary>
    public abstract class Car_RentalRepositoryBase : RepositoryBase, IRepository
    {
        static string _conn;
        static Car_RentalRepositoryBase()
        {
            if (ConfigurationManager.ConnectionStrings["Car_Rental"] == null)
                throw new Exception("请设置Background配置字符");
            else
                _conn = ConfigurationManager.ConnectionStrings["Car_Rental"].ToString();
        }
        public Car_RentalRepositoryBase()
            : base(Data.DbFactory.Intance(_conn))
        { }


        #region IRepository 成员

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Update(EntityBase entity)
        {
            base.UpdateEntity(entity);
        }

        /// <summary>
        /// 更新实体列表
        /// </summary>
        /// <param name="list"></param>
        public virtual void Update(IList<EntityBase> list)
        {
            base.UpdateEntity(list.ToList());
        }

        /// <summary>
        /// 插入实体
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Insert(EntityBase entity)
        {
            base.InsertEntity(entity);
        }

        /// <summary>
        /// 插入实体列表
        /// </summary>
        /// <param name="list"></param>
        public virtual void Insert(IList<EntityBase> list)
        {
            base.InsertEntity(list.ToList());
        }

        /// <summary>
        /// 插入实体并返回实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual TEntity InsertGetIDENTITY<TEntity>(EntityBase entity) where TEntity : EntityBase
        {
            base.InsertEntity(entity);
            return this.Find<TEntity>(entity.PrimaryKey);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Delete(EntityBase entity)
        {
            base.DeleteEntity(entity);
        }

        /// <summary>
        /// 删除实体列表 
        /// </summary>
        /// <param name="list"></param>
        public virtual void Delete(IList<EntityBase> list)
        {
            base.DeleteEntity(list.ToList());
        }

        /// <summary>
        /// 重到结果集
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<TEntity> GetModel<TEntity>() where TEntity : EntityBase
        {
            return base.GetEntities<TEntity>();
        }

        /// <summary>
        /// 根据条件，重到结果集
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<TEntity> GetModel<TEntity>(Expression<Func<EntityBase, bool>> predicate) where TEntity : EntityBase
        {
            return this.GetModel<TEntity>().Where(predicate).Cast<TEntity>();
        }

        /// <summary>
        /// 根据主键得到实体
        /// </summary>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public virtual TEntity Find<TEntity>(params object[] keyValues) where TEntity : EntityBase
        {
            return base.GetEntity<TEntity>(keyValues);
        }

        #endregion
         
    }
}
